<?php

namespace addons\yungift\model;

use addons\yungift\library\Logic;
use addons\yungift\library\ModelTrait;
use think\Model;
use traits\model\SoftDelete;

/**
 * 礼品 - 兑换订单
 */
class GiftOrder extends Model
{
    use ModelTrait;
    use SoftDelete;

    // 表名,不含前缀
    protected $name = 'yungift_gift_order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $deleteTime = 'deleted_at';
    protected $updateTime = 'updated_at';
    protected $createTime = 'created_at';

    // 不显示的字段
    protected $hidden = ['deleted_at'];

    protected $resultSetType = 'collection';

    // 自定义初始化
    protected static function init()
    {
        GiftOrder::event('before_insert', function ($item) {
            // 对象默认值
//            Logic::setDefaultValue($item, [
//            ]);

            return true;
        });

        GiftOrder::event('after_insert', function ($item) {
            // 对象默认值
            return true;
        });
    }

    // 活动数据
    public function activityData()
    {
        return $this->belongsTo(Activity::class, 'model_id', 'id');
    }

    // 礼卡数据
    public function cardData()
    {
        return $this->belongsTo(CardMain::class, 'model_id', 'id');
    }

    // 卡密数据
    public function cardCodeData()
    {
        return $this->belongsTo(CardCode::class, 'account_id', 'id');
    }

    // 成员数据
    public function memberData()
    {
        return $this->belongsTo(Member::class, 'account_id', 'id');
    }

    // 礼品数据
    public function giftData()
    {
        return $this->belongsTo(GiftMain::class, 'gift_id', 'id');
    }

    // 礼品规格数据
    public function giftSpecData()
    {
        return $this->belongsTo(GiftSpec::class, 'gift_spec_id', 'id');
    }

    // 类型
    const TYPE_ACTIVITY = 'activity';
    const TYPE_CARD     = 'card';

    public static function getTypeList()
    {
        return [
            // self::TYPE_ACTIVITY => '礼品活动',
            self::TYPE_CARD     => '礼卡',
        ];
    }

    public function getTypeTextAttr($value, $data)
    {
        $optionList = self::getTypeList();

        return $optionList[$data['type']] ?? $data['type'];
    }

    public function getModelTextAttr($value, $data)
    {
        if ($data['type'] == self::TYPE_ACTIVITY) {
            if ($activity = $this->activityData) {
                return $activity->name;
            }
        } elseif ($data['type'] == self::TYPE_CARD) {
            if ($card = $this->cardData) {
                return $card->name;
            }
        }

        return $data['model_id'];
    }

    // 礼品类型
    public static function getGiftTypeList()
    {
        return GiftMain::getTypeList();
    }

    public function getGiftTypeTextAttr($value, $data)
    {
        $optionList = self::getGiftTypeList();

        return $optionList[$data['gift_type']] ?? $data['gift_type'];
    }

    // 领取方式
    const GAIN_METHOD_SELF    = 1;
    const GAIN_METHOD_EXPRESS = 2;
    const GAIN_METHOD_NONE    = 3;

    public static function getGainMethodList($deploy = false)
    {
        $options = [
            self::GAIN_METHOD_SELF    => '线下领取',
            self::GAIN_METHOD_EXPRESS => '快递邮寄',
            self::GAIN_METHOD_NONE    => '直接发放',
        ];
        if ($deploy) {
            unset($options[self::GAIN_METHOD_NONE]);
        }

        return $options;
    }

    public function getGainMethodTextAttr($value, $data)
    {
        $optionList = self::getGainMethodList();

        return $optionList[$data['gain_method']] ?? $data['gain_method'];
    }

    // 状态
    const STATUS_WAIT_DEPLOY = 1;
    const STATUS_HAS_DEPLOY  = 2;
    const STATUS_HAS_BACK    = 3;

    public static function getStatusList()
    {
        return [
            self::STATUS_WAIT_DEPLOY => '待发货',
            self::STATUS_HAS_DEPLOY  => '已发货',
            self::STATUS_HAS_BACK    => '已取消',
        ];
    }

    public function getStatusTextAttr($value, $data)
    {
        $optionList = self::getStatusList();

        return $optionList[$data['status']] ?? $data['status'];
    }

    public function getExpressCompanyTextAttr($value, $data)
    {
        $optionList = Logic::queryExpressCompany();

        return $optionList[$data['express_company']] ?? $data['express_company'];
    }

    public function getGiftDataArrayAttr($value, $data)
    {
        $giftData = json_decode($data['gift_data'], true);
        $giftData['cover_url'] = cdnurl($giftData['cover_url'], true);

        return $giftData;
    }
}
